package com.atguigu.gmall.sms.service.impl;

import com.atguigu.gmall.sms.entity.SkuBoundsEntity;
import com.atguigu.gmall.sms.entity.SkuFullReductionEntity;
import com.atguigu.gmall.sms.service.SkuBoundsService;
import com.atguigu.gmall.sms.service.SkuFullReductionService;
import com.atguigu.gmall.sms.vo.ItemSaleVO;
import com.atguigu.gmall.sms.vo.SkuSaleVO;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.sms.mapper.SkuLadderMapper;
import com.atguigu.gmall.sms.entity.SkuLadderEntity;
import com.atguigu.gmall.sms.service.SkuLadderService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("skuLadderService")
public class SkuLadderServiceImpl extends ServiceImpl<SkuLadderMapper, SkuLadderEntity> implements SkuLadderService {
    @Resource
    SkuBoundsService skuBoundsService;
    @Resource
    SkuFullReductionService skuFullReductionService;
    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<SkuLadderEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<SkuLadderEntity>()
        );

        return new PageResultVo(page);
    }
    @Transactional
    @Override
    public void saveSkuSales(SkuSaleVO skuSaleVO) {
        //满几件 打几折
        SkuLadderEntity skuLadder = new SkuLadderEntity();
        skuLadder.setSkuId(skuSaleVO.getSkuId());
        skuLadder.setDiscount(skuSaleVO.getDiscount());
        skuLadder.setFullCount(skuSaleVO.getFullCount());
        skuLadder.setAddOther(skuSaleVO.getLadderAddOther());
        this.save(skuLadder);
//        int i = 1/0;
        //积分
        SkuBoundsEntity skuBounds = new SkuBoundsEntity();
        skuBounds.setSkuId(skuSaleVO.getSkuId());
        skuBounds.setGrowBounds(skuSaleVO.getGrowBounds());
        skuBounds.setBuyBounds(skuSaleVO.getBuyBounds());
        /*
            优惠生效情况[1111（四个状态位，从右到左）;
                    8 4 2 1
            0 - 无优惠，成长积分是否赠送;
            1 - 无优惠，购物积分是否赠送;
            2 - 有优惠，成长积分是否赠送;
            3 - 有优惠，购物积分是否赠送【状态位0：不赠送，1：赠送】]
         */
        skuBounds.setWork(skuSaleVO.getWork().get(0)*8 + skuSaleVO.getWork().get(1)*4 +
                skuSaleVO.getWork().get(2)*2  + skuSaleVO.getWork().get(3)*1);
        skuBoundsService.save(skuBounds);

        //满减
        SkuFullReductionEntity skuFullReduction = new SkuFullReductionEntity();
        skuFullReduction.setSkuId(skuSaleVO.getSkuId());
        skuFullReduction.setReducePrice(skuSaleVO.getReducePrice());
        skuFullReduction.setFullPrice(skuSaleVO.getFullPrice());
        skuFullReduction.setAddOther(skuSaleVO.getFullAddOther());
        skuFullReductionService.save(skuFullReduction);
    }

    @Override
    public List<ItemSaleVO> querySkuSalesBySkuId(Long skuId) {
        List<ItemSaleVO> itemSaleVOS = new ArrayList<>();
        //1、折扣：  满2件,8折优惠
        List<SkuLadderEntity> skuLadderEntities = this.list(Wrappers.lambdaQuery(SkuLadderEntity.class)
                .eq(SkuLadderEntity::getSkuId, skuId));
        if(!CollectionUtils.isEmpty(skuLadderEntities)){
            itemSaleVOS.addAll(skuLadderEntities.stream().map(skuLadderEntity -> {
                ItemSaleVO itemSaleVO = new ItemSaleVO();
                itemSaleVO.setSaleId(skuLadderEntity.getId());
                itemSaleVO.setType("折扣");
                itemSaleVO.setDesc("满"+skuLadderEntity.getFullCount()+"件,"+
                        skuLadderEntity.getDiscount().divide(new BigDecimal("10"),1,BigDecimal.ROUND_DOWN)+
                        "折优惠");
                return itemSaleVO;
            }).collect(Collectors.toList()));
        }

        //2、满减
        List<SkuFullReductionEntity> skuFullReductionEntities = skuFullReductionService.list(Wrappers.lambdaQuery(SkuFullReductionEntity.class)
                .eq(SkuFullReductionEntity::getSkuId, skuId));
        if(!CollectionUtils.isEmpty(skuFullReductionEntities)){
            itemSaleVOS.addAll(skuFullReductionEntities.stream().map(skuFullReductionEntity -> {
                ItemSaleVO itemSaleVO = new ItemSaleVO();
                itemSaleVO.setSaleId(skuFullReductionEntity.getId());
                itemSaleVO.setType("满减");
                itemSaleVO.setDesc("满"+skuFullReductionEntity.getFullPrice().intValue()+"元减"+
                            skuFullReductionEntity.getReducePrice().intValue()+"元");
                return itemSaleVO;
            }).collect(Collectors.toList()));
        }
        //3、积分
        List<SkuBoundsEntity> skuBoundsEntities = skuBoundsService.list(Wrappers.lambdaQuery(SkuBoundsEntity.class)
                .eq(SkuBoundsEntity::getSkuId, skuId));
        if(!CollectionUtils.isEmpty(skuBoundsEntities)){
            itemSaleVOS.addAll(skuBoundsEntities.stream().map(skuBoundsEntity -> {
                ItemSaleVO itemSaleVO = new ItemSaleVO();
                itemSaleVO.setSaleId(skuBoundsEntity.getId());
                itemSaleVO.setType("积分");
                itemSaleVO.setDesc("赠送"+skuBoundsEntity.getBuyBounds().intValue()+"购物积分,"+
                        skuBoundsEntity.getGrowBounds().intValue()+"成长积分");
                return itemSaleVO;
            }).collect(Collectors.toList()));
        }
        return itemSaleVOS;
    }

}